package com.zhumo.sz.controller;

import com.zhumo.sz.common.CommonInsert;
import com.zhumo.sz.common.Result;
import com.zhumo.sz.entity.subway.*;
import com.zhumo.sz.service.SubwayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author 地铁业务控制层
 * @time 2024-03-03
 * @comment 业务流程 发送请求 -> 请求数据平台 -> 解析结果 -> 插入数据库
 */
@Slf4j
@RestController
@RequestMapping("/sz/service/subway")
public class SubwayController {

    @Autowired
    private SubwayService subwayService;

    private static final String inertError = "插入失败";

    /**
     * 插入地铁站点信息
     */
    @GetMapping("/station")
    public Result<String> setSubwayStationInfo(@RequestParam int page,@RequestParam int rows){
        //需要获取到service的结果进行校验
        CommonInsert<SubwayStationInfo> subwayStationInfoCommonInsert = subwayService.setSubwayStationInfo(page, rows);
        int num = subwayStationInfoCommonInsert.getNum();
        if (num == subwayStationInfoCommonInsert.getResult()){
            String message = "成功插入" + num + "条地铁站点信息";
            log.info(message);
            return Result.ok(message);
        }else {
            log.error(inertError);
            return Result.fail(inertError);
        }
    }

    /**
     * 插入地铁线路信息
     */
    @GetMapping("/line")
    public Result<String> setSubwayLineInfo(){
        CommonInsert<SubwayLine> subwayLineCommonInsert = subwayService.setSubwayLine();
        int num = subwayLineCommonInsert.getNum();
        if(num == subwayLineCommonInsert.getResult()){
            String message = "成功插入" + num + "条地铁线路信息";
            log.info(message);
            return Result.ok(message);
        }else {
            log.error(inertError);
            return Result.fail(inertError);
        }
    }


    /**
     * 插入地铁公交站换乘信息
     */
    @GetMapping("/tranfer")
    public Result<String> setSubwayTransitTransfer(@RequestParam int page,@RequestParam int row){
        CommonInsert<SubwayTransitTransfer> transferCommonInsert = subwayService.setSubwayTransitTransfer(page, row);
        int num = transferCommonInsert.getNum();
        if (num == transferCommonInsert.getResult()){
            String message = "成功插入" + num + "条地铁出口公交换乘信息";
            log.info(message);
            return Result.ok(message);
        }else {
            log.error(inertError);
            return Result.fail(inertError);
        }
    }


    /**
     * 插入地铁流量信息 type batch 为批处理 real 为实时生成数据
     */
    @GetMapping("/traffic")
    public Result<String> mockSubwayTraffic(@RequestParam int num,@RequestParam String date,@RequestParam String type){
        CommonInsert<SubwayGateSwipeDetail> trafficCommon = subwayService.mockSubwayTraffic(num, date, type);
        String message = "成功插入" + trafficCommon.getResult() + "条地铁流量信息";
        log.info(message);
        return Result.ok(message);
    }

    /**
     * 插入闸机信息
     */
    @GetMapping("/gate")
    public Result<String> addGateInfo(){
        CommonInsert<SubwayGateInfo> gateInfoCommonInsert = subwayService.addGate();
        int num = gateInfoCommonInsert.getNum();
        if(num == gateInfoCommonInsert.getResult()){
            String message = "成功插入" + num + "条闸机信息";
            log.info(message);
            return Result.ok(message);
        }else {
            log.error(inertError);
            return Result.fail(inertError);
        }
    }

    /**
     *插入地铁营运时刻信息
     */
    public Result<String> setSubwayOperatorHour(){
        return Result.ok("ok");
    }

}
